<template>
  <!-- 发现好歌单 -->
  <div class="musicList">
    <div class="musicTop">
      <div class="title">发现好歌单</div>
      <div class="more">查看更多</div>
    </div>
    <div class="musicContent">
      <van-swipe :loop="false" :width="150" class="my-swipe" :show-indicators="false">
        <van-swipe-item v-for="item in state.musicList" :key="item" class="van-swipe-item">
          <router-link :to="{ path: '/ItmeMusic', query: { id: item.id } }">
            <!-- 图片 -->
            <img :src="item.picUrl" alt="" />
            <!-- 图标 -->
            <span class="playCount">
              <div class="icon">
                <img src="@/assets/img/播放.svg" alt="" />
              </div>
              {{ changeCount(item.playCount) }}
            </span>
            <!-- 文字 -->
            <span class="name">{{ item.name }}</span>
          </router-link>
        </van-swipe-item>
      </van-swipe>
    </div>
  </div>
</template>

<script>
// 引入 getMusicList 函数
import { getMusicList } from '@/request/api/home.js'
import { reactive, onMounted } from 'vue'

export default {
  // vue2 写法
  /*   data() {
    return {
      musicList: [],
    }
  },
  methods: {
    async getGender() {
      const res = await getMusicList()
      this.musicList = res.data.result
    },
    changeCount: function (num) {
      if (num >= 10000000) {
        return (num / 10000000).toFixed(1) + '亿'
      } else if (num / 10000) {
        return (num / 10000).toFixed(1) + '万'
      }
    },
  },

  // 生命周期
  mounted() {
    this.getGender()
  },
  */

  // vue3 写法
  setup() {
    const state = reactive({
      musicList: [],
    })
    // 播放量
    function changeCount(num) {
      if (num >= 100000000) {
        return (num / 100000000).toFixed(1) + '亿'
      } else if (num >= 10000) {
        return (num / 10000).toFixed(1) + '万'
      }
    }
    onMounted(async () => {
      let res = await getMusicList()
      // console.log(res)
      state.musicList = res.data.result
    })
    return { state, changeCount }
  },
}
</script>

<style lang="less" scoped>
.musicList {
  width: 100%;
  height: 5rem;
  padding: 0.2rem;
  .musicTop {
    width: 100%;
    height: 0.8rem;
    display: flex;
    justify-content: space-between;
    margin-bottom: 0.2rem;
    margin-top: 3rem;
    .title {
      font-size: 1.3rem;
      font-weight: 900;
    }
    .more {
      border: 1px solid #ccc;
      font-size: 1rem;
      text-align: center;
      line-height: 0.8rem;
      padding: 0 0.5rem;
      border-radius: 0.4rem;
      padding-top: 0.5rem;
    }
  }
  .musicContent {
    width: 100%;
    height: 13rem;
    margin-top: 3rem;

    .van-swipe-item {
      //   margin-right: 0.14rem;
      padding-right: 0.5rem;
      position: relative;
      height: 13rem;
      img {
        width: 100%;
        height: 9rem;
        border-radius: 0.6rem;
        //   position: absolute;
      }
      .playCount {
        position: absolute;
        z-index: 100;
        right: 0.3rem;
        color: white;
        margin-top: 0.06rem;
        .icon {
          width: 0.3rem;
          height: 0.3rem;
        }
      }
      .name {
        //   position: absolute;
        bottom: 0px;
      }
    }
  }
}

</style>
